package com.iweb.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class JSONWriter {

    /**
     * 将json数据发送给客户端
     *
     * @param resp
     * @param o
     */
    public static void send(HttpServletResponse resp, Object o) {
        PrintWriter writer = null;
        try {
            String jsonStr = JSONUtil.toJsonPrettyStr(o == null ? "{}" : JSON.toJSONString(o));
            writer = resp.getWriter();
            writer.println(jsonStr);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }

}
